import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { JsonResult } from '../model';
import { HttpClient } from '@angular/common/http';
import { delay,tap, map } from 'rxjs/operators';
import { Router } from '@angular/router';
import * as gc from 'src/app/global/const';

@Injectable({
  providedIn: 'root'
})
export class AuthService {

  isLoggedIn = false;
  redirectUrl: string;
  user:SysUser = null;

  urlPre = "/login";
  url = {
    'login':gc.BACKEND_SERVER + this.urlPre + '/in',
    'logout':gc.BACKEND_SERVER + this.urlPre + '/out'
  }

  constructor(
    private http:HttpClient,
    private router:Router
  ) { }

  login():Observable<JsonResult>{
    const url = "https://www.xdfznh.club/atoolsbe/login/in";
    return this.http.post<JsonResult>(url,{'username':'1002','password':'7758'}).pipe(map(
      ( jr:JsonResult ) =>{

        this.isLoggedIn = true;
        this.router.navigate(['/layout']);
        return jr;
      }
    ))
  }

  logout():Observable<JsonResult>{
    const url = "https://www.xdfznh.club/atoolsbe/login/out";
    return this.http.post<JsonResult>(url,null).pipe(map(
      ( jr:JsonResult ) =>{
        this.isLoggedIn = false;
        this.router.navigate(['/login']);
        return jr;
      }
    ))
  }
}


export class SysUser{
  create_time:Date;
  iid:number;
  last_time:Date;
  non_expired:boolean;
  non_locked:boolean;
  non_password_expired:boolean;
  password:string;
  remark:string;
  status:number;
  userid:string;
  username:string;
}